package com.qhsx.asset.service.impl;

import com.qhsx.asset.domain.*;
import com.qhsx.asset.mapper.*;
import com.qhsx.asset.service.AsyncService;
import com.qhsx.asset.service.IAssetProductService;
import com.qhsx.common.utils.SecurityUtils;
import com.qhsx.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 无形资产Service业务层处理
 *
 * @author yepanpan
 * @date 2021-03-05
 */
@Service
public class AssetProductServiceImpl implements IAssetProductService
{
    @Autowired
    private AssetProductMapper assetProductMapper;

    @Autowired
    AssetEnteringMapper assetEnteringMapper;

    @Autowired
    AsyncService asyncService;
    @Autowired
    AssetCategoryMapper assetCategoryMapper;
    @Autowired
    AssetWarehouseMapper assetWarehouseMapper;

    @Override
    public  List<AssetProduct>  list(AssetProduct assetProduct){
        return assetProductMapper.list(assetProduct);
    }

    @Override
    public  void  initBatch(){
        List<AssetEntering> assetEnteringList = assetEnteringMapper.selectAssetEnteringList(new AssetEntering());
        for (AssetEntering assetEntering:assetEnteringList){
            AssetProduct assetProduct=new AssetProduct();
            assetProduct.setEnteringId(assetEntering.getId());
            List<AssetProduct> assetProductList = assetProductMapper.list(assetProduct);
            if(assetProductList.size()==0){
                //生成产品
                batch(assetEntering);
            }
        }
    }

    @Override
    public  List<AssetProduct>  batch(AssetEntering assetEntering){
        //Long lastProduct = assetProductMapper.getLastProduct();
        int count = assetEntering.getAmount().intValue();
        List<AssetProduct> productList=new ArrayList<>();
        AssetCategory assetCategory = assetCategoryMapper.selectAssetCategoryById(assetEntering.getCateId());
        assetEntering.setCate(assetCategory);
        for (int i=0;i<count;i++){
            //lastProduct += i;
            String handle="86.122.2/"+ IdUtils.simpleTimestamp();
            asyncService.regIden(handle,assetEntering);
            AssetProduct assetProduct=new AssetProduct();
            assetProduct.setHandle(handle);
            assetProduct.setEnteringId(assetEntering.getId());
            assetProduct.setCreateBy(SecurityUtils.getUsername());
            assetProduct.setCateId(assetEntering.getCateId());
            assetProduct.setMaterialId(assetEntering.getRelationId());
            productList.add(assetProduct);
        }
        assetProductMapper.insertBach(productList);
        //productList.stream().forEach(item->{
        //    item.setHandle(item.getHandle()+item.getId().toString());
        //});
        assetProductMapper.updateBach(productList);
        return productList;
    }

    @Override
    public  void leave(String handle){
        assetProductMapper.leave(SecurityUtils.getUsername(),handle);
    }

    @Override
    public  Long id(String handle){
        return  assetProductMapper.selectIdByHandle(handle);
    }


}
